package com.lsy.weizhi2.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lsy.weizhi2.JsonObject.Classify;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.customview.MyGridView;
import com.lsy.weizhi2.myutils.CustomLVAdapterCall;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/17.
 */
public class CustomPage_LV_Adapter extends BaseAdapter {
    private Context context;
    private List<Classify> list;
    private List<Classify.SecondCategoriesBean>list_s;
    private LayoutInflater layoutInflater;
    private CustomLVAdapterCall customLVAdapterCall;
    public CustomPage_LV_Adapter(Context context, List<Classify> list) {
        this.context = context;
        this.list = list;
        layoutInflater=LayoutInflater.from(context);
    }

    public void UpData(List<Classify> list){
        this.list=list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Custom_LV_Holder holder;
        if(view==null){
            view=layoutInflater.inflate(R.layout.custom_lv_item,viewGroup,false);
            holder=new Custom_LV_Holder();
            holder.custom_biaoqian= (TextView) view.findViewById(R.id.custom_biaoqian);
            holder.custom_mgv= (MyGridView) view.findViewById(R.id.custom_mgv);
            AutoUtils.autoSize(view);
            view.setTag(holder);
        }else{
            holder= (Custom_LV_Holder) view.getTag();
        }
        holder.custom_biaoqian.setText(list.get(i).getName());
        Log.i("CustomAdapter","i==="+i+"\nname=="+list.get(i).getName());
        Drawable drawable=context.getResources().getDrawable(R.mipmap.biaoqian_ico_01);
        holder.custom_biaoqian.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);
        if(list!=null){
            list_s=list.get(i).getSecondCategories();
            Log.i("CustomAdapter","list_s"+list_s.size()+"\nname=="+list.get(i).getName());
        }
        GridItemAdapter gridItemAdapter=new GridItemAdapter(list_s);
        holder.custom_mgv.setAdapter(gridItemAdapter);
        return view;
    }

    public void setCustomLVAdapterCall(CustomLVAdapterCall customLVAdapterCall){
        this.customLVAdapterCall=customLVAdapterCall;
    }

    class Custom_LV_Holder{
        TextView custom_biaoqian;
        MyGridView custom_mgv;
    }

    class GridItemAdapter extends BaseAdapter{
        private List<Classify.SecondCategoriesBean>list_s;
        public GridItemAdapter(List<Classify.SecondCategoriesBean>list_s) {
                this.list_s=list_s;
        }

        @Override
        public int getCount() {
            return list_s==null?0:list_s.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            GridItemHolder holder;
            if(view==null){
                view = layoutInflater.inflate(R.layout.text_view,viewGroup,false);
                holder=new GridItemHolder();
                holder.textView= (TextView) view.findViewById(R.id.textView);
                AutoUtils.autoSize(view);
                view.setTag(holder);
            }else{
                holder= (GridItemHolder) view.getTag();
            }
            holder.textView.setText(list_s.get(i).getName());
            if(list_s.get(i).getName().equals("企业招聘")||list_s.get(i).getName().equals("工作求职")||list_s.get(i).getName().equals("兼职短聘")){
                holder.textView.setTextColor(context.getResources().getColor(R.color.hot));
            }
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("CustomAdapter","second==="+list_s.get(i).getId()+"\nfirst==="+list_s.get(i).getFid()+"\ntitle==="+list_s.get(i).getName());
                    customLVAdapterCall.onItem(v,list_s.get(i).getId(),list_s.get(i).getFid(),list_s.get(i).getName());
                }
            });
            return view;
        }

        class GridItemHolder{
            TextView textView;
        }
    }
}
